import Vue from 'vue'
import Vuex from 'vuex'
import vuex_user from './modules/user'
import getters from './getters'

Vue.use(Vuex)

const store = new Vuex.Store({
    modules: {
        vuex_user,
    },
    state: {
        vuex_tabbars: [{
            "pagePath": "/pages/main/main",
            "text": "工作台",
            "iconPath": "/static/img/home.png",
            "selectedIconPath": "/static/img/homeHL.png"
        }, {
            "pagePath": "/pages/kmsManagement/kms",
            "text": "知识库",
            "iconPath": "/static/img/kms.png",
            "selectedIconPath": "/static/img/kmsHL.png"
        }, {
            "pagePath": "/pages/user/user",
            "text": "我",
            "iconPath": "/static/img/user.png",
            "selectedIconPath": "/static/img/userHL.png"
        }],
        vuex_keyboardStatus: false,
    },
    mutations: {
        $uStore(state, payload) {
            // 判断是否多层级调用，state中为对象存在的情况，诸如user.info.score = 1
            let nameArr = payload.name.split('.');
            let saveKey = '';
            let len = nameArr.length;
            if (nameArr.length >= 2) {
                let obj = state[nameArr[0]];
                for (let i = 1; i < len - 1; i++) {
                    obj = obj[nameArr[i]];
                }
                obj[nameArr[len - 1]] = payload.value;
                saveKey = nameArr[0];
            } else {
                // 单层级变量，在state就是一个普通变量的情况
                state[payload.name] = payload.value;
                saveKey = payload.name;
            }
            // 保存变量到本地，见顶部函数定义
        }
    },
    getters
})

export default store
